import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/model/course_complete_info.dart';
import 'package:kxy_flutter_app/r.dart';

class CompletedItemWidget extends StatelessWidget {
  final CourseCompleteInfo info;
  final int index;
  const CompletedItemWidget({
    Key key,
    @required this.info,
    @required this.index,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.black12,
            width: 0.5,
          ),
        ),
      ),
      child: ListTile(
        leading: getNumber(),
        title: getName(),
        subtitle: getSubTitle(),
        trailing: getStatusWidget(),
      ),
    );
  }

  Widget getNumber() {
    var text = '';
    if (index < 10) {
      text = '00${index + 1}';
    } else if (index < 100) {
      text = '0${index + 1}';
    } else {
      text = index.toString();
    }

    return Text(
      text,
      style: TextStyle(
        color: Colors.black54,
        fontSize: 18,
        fontWeight: FontWeight.bold,
      ),
    );
  }

  Widget getName() {
    return Text(
      info.realName,
      style: TextStyle(
        fontSize: 18,
        color: Colors.black87,
      ),
    );
  }

  Widget getSubTitle() {
    var textStyle = TextStyle(
      fontSize: 13,
      color: Colors.black54,
    );
    var format = new DateFormat('yyyy-MM-dd');
    return Row(
      children: [
        Image.asset(
          R.assetsCommonDept,
          height: 12,
          color: Colors.black38,
        ),
        Expanded(
          child: Text(
            info.deptName,
            style: textStyle,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        if (info.status == 2)
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 5),
            child: Text('|', style: textStyle),
          ),
        if (info.status == 2) Text('${format.format(info.finishTime)}完成', style: textStyle),
      ],
    );
  }

  Widget getStatusWidget() {
    var status = info.status;
    Color color;
    String text;

    if (status == -1) {
      color = Colors.black54;
      text = '无权限';
    }

    if (status == 0) {
      color = Color.fromRGBO(255, 199, 110, 1);
      text = '未学习';
    }

    if (status == 1) {
      color = Color.fromRGBO(40, 196, 122, 1);
      text = '未完成';
    }

    if (status == 2) {
      color = Color.fromRGBO(63, 145, 252, 1);
      text = '已完成';
    }

    return Text(
      text,
      style: TextStyle(
        color: color,
        fontSize: 13,
      ),
    );
  }
}
